// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) radix(10) lradix(10) 
// Source File Name:   LicenseManager.java

package cn.firstsoft.core.license;

import java.io.File;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.logging.Logger;
import org.springside.core.license.LicenseSerial;

// Referenced classes of package cn.firstsoft.core.license:
//            ILicenseCode, LicenseFileErrorException, License, LicenseImpl, 
//            Utils, HashAlgorithm, CryptoAlgorithm, RSA

public class LicenseManager implements ILicenseCode {
	private static class MD5HashAlgorithm extends HashAlgorithm {

		public byte[] hash(byte bytes[]) {
			try {
				MessageDigest md = MessageDigest.getInstance("MD5");
				return md.digest(bytes);
			} catch (NoSuchAlgorithmException e) {
				throw new Error("Cann't find MD5 Alogorithm to hash the value!");
			}
		}

		private MD5HashAlgorithm() {
		}

		MD5HashAlgorithm(MD5HashAlgorithm md5hashalgorithm) {
			this();
		}
	}

	private static class RSACryptoAlgorithm extends CryptoAlgorithm {

		public byte[] encrypt(byte bytes[]) {
			return RSA.encrypt(bytes);
		}

		public byte[] decrypt(byte bytes[]) {
			return RSA.decrypt(bytes);
		}

		private RSACryptoAlgorithm() {
		}

		RSACryptoAlgorithm(RSACryptoAlgorithm rsacryptoalgorithm) {
			this();
		}
	}

	public LicenseManager() {
	}

	public static License getLicense() throws LicenseFileErrorException {
		//String fileName = (new StringBuilder(String.valueOf(System.getProperty("oa_application.config")))).append(File.separator).append("license").toString();
		String fileName = "D:\\myeclipsework\\Kingdeeoa\\license";
		return getLicense(fileName, "kingdee eas ecp","24B093EE1CB78CD3EEF7929046A6BC7B");
	}

	public static License getLicense(String fileName, String softwareName)
			throws LicenseFileErrorException {
		return getLicense(fileName, softwareName, null);
	}

	public static License getLicense(String fileName, String softwareName,
			String password) throws LicenseFileErrorException {
		String codes[] = getSoftwareCodes();
		License rtnLicense = null;
		int i = 0;
		for (int n = codes.length; i < n; i++) {
			License l = getLicense(fileName, softwareName, password, codes[i]);
			if (!l.isRegistered() && i != n - 1)
				continue;
			rtnLicense = l;
			break;
		}

		return rtnLicense;
	}

	public static License getLicense(String fileName, String softwareName,
			String password, String softwareCode)
			throws LicenseFileErrorException {
		License license = new LicenseImpl(ca, ha);
		license.setSoftwareName(softwareName);
		license.setSoftwareCode(softwareCode);
		license.setPassword(password);
		license.readLicense(fileName);
		return license;
	}

	public static String[] getSoftwareCodes() throws LicenseFileErrorException {
		List localIpList = getLocalMAC();
		List localCodes = new ArrayList();
		int i = 0;
		for (int n = localIpList.size(); i < n; i++) {
			String ip = (String) localIpList.get(i);
			if (ip != null && ip.trim().length() > 0) {
				String softwareCode = Utils.hash(ha, ip);
				softwareCode = softwareCode.substring(0, 16);
				localCodes.add(softwareCode);
			}
		}

		if (localIpList.size() == 0 || localCodes.size() == 0) {
			String defaultMAC = "00-00-00-00-00-00-00-00";
			String softwareCode = Utils.hash(ha, defaultMAC);
			softwareCode = softwareCode.substring(0, 16);
			localCodes.add(softwareCode);
		}
		String rtn[] = new String[localCodes.size()];
		return (String[]) localCodes.toArray(rtn);
	}

	private static List getLocalIP() throws LicenseFileErrorException {
		List list = new ArrayList();
		try {
			for (Enumeration enu = NetworkInterface.getNetworkInterfaces(); enu
					.hasMoreElements();) {
				NetworkInterface ni = (NetworkInterface) enu.nextElement();
				for (Enumeration ias = ni.getInetAddresses(); ias
						.hasMoreElements();) {
					InetAddress localAddress = (InetAddress) ias.nextElement();
					String hostAddress = localAddress.getHostAddress();
					if (hostAddress != null) {
						String ips[] = hostAddress.split("\\.");
						if (ips.length == 4) {
							list.add(hostAddress);
							logger.info((new StringBuilder("find ip address:"))
									.append(hostAddress).toString());
						}
					}
				}

			}

			if (list.size() == 0) {
				list.add("127.0.0.1");
				logger.info("there is no network");
				logger.info("fill ip address:127.0.0.1");
			}
			return list;
		} catch (Exception e) {
			throw new LicenseFileErrorException(1003);
		}
	}

	private static List getLocalMAC() {
		List list = new ArrayList();
		String macStr = LicenseSerial.getMacAddress();
		String macs[] = macStr.split("#");
		String as[];
		int j = (as = macs).length;
		for (int i = 0; i < j; i++) {
			String mac = as[i];
			list.add(mac);
		}

		return list;
	}


	public String getSoftCode(String ip) {
		if (ip == null || ip.equalsIgnoreCase("")
				|| ip.split("\\.").length != 4)
			return null;
		else
			return Utils.hash(ha, ip).substring(0, 16);
	}

	public String[] getMachineAllSoftCodes() throws LicenseFileErrorException {
		return getSoftwareCodes();
	}
	
	//测试
	public static void main(String args[]) throws LicenseFileErrorException {
		LicenseManager.getLicense();
		String encrydate=dealData("2019-10-10 21:00:00");//2019-10-10 21:00:00
		System.out.println(encrydate);
		//encrydate="032F15BA0DF40E5B11FE15BA0DF411FE15BA0DF405F0032F0DF406C615BA15BA06C615BA15BAF8E75058CCF897CE";
		Date mydate=checkDateData("",encrydate);
	}
	//=========================增加验证处理===========================
	private static String encrypt(String source) {
		try {
			return new String(ca.encrypt(source.getBytes("ISO8859-1")),
					"ISO8859-1");
		} catch (Exception ex) {
			throw new Error(
					(new StringBuilder("UnsupportedEncodingException:"))
							.append(ex.getMessage()).toString());
		}
	}
	private static String hash(String source) {
		source = (new StringBuilder(String.valueOf(source))).append(password)
				.toString();
		return Utils.hash(ha, source);
	}
	
	private static String checkHash(String label, String pStr)
			throws LicenseFileErrorException {
		int len = pStr.length();
		if (len <= 16)
			throw new LicenseFileErrorException(1000, label);
		String enryptedPart = pStr.substring(0, len - 16);
		String hashPart = pStr.substring(len - 16);
		String hashValue = hash(enryptedPart);
		if (!hashPart.equals(hashValue.substring(0, 16)))
			throw new LicenseFileErrorException(1000, label);
		else
			return enryptedPart;
	}
	
	private static String decrypt(String dest) {
		try {
			return new String(ca.decrypt(dest.getBytes("ISO8859-1")),
					"ISO8859-1");
		} catch (Exception ex) {
			throw new Error(
					(new StringBuilder("UnsupportedEncodingException:"))
							.append(ex.getMessage()).toString());
		}
	}

	
	private static Date checkDateData(String label, String pStr)
			throws LicenseFileErrorException {
		if (pStr == null)
			return null;
		String value = checkHash(label, pStr);
		String date = decrypt(value);
		try {
			return Utils.parseDate(date);
		} catch (Exception e) {
			throw new LicenseFileErrorException(1000, "ExpireDate", e);
		}
	}
	
	//时间加密处理
	private static String dealData(String data) {
		String c = encrypt(data);
		String hc = hash(c).substring(0, 16);
		return (new StringBuilder(String.valueOf(c))).append(hc).toString();
	}

	private String dealData(Integer data) {
		String c = String.valueOf(data);
		return dealData(c);
	}

	private String dealData(Date data) {
		String d = Utils.formatDate(data);
		return dealData(d);
	}
	
	
	private static String password="24B093EE1CB78CD3EEF7929046A6BC7B";
	private static HashAlgorithm ha = new MD5HashAlgorithm(null);
	private static CryptoAlgorithm ca = new RSACryptoAlgorithm(null);
	private static Logger logger = Logger
			.getLogger("cn.firstsoft.core.license.LicenseManager");

}


/*
	DECOMPILATION REPORT

	Decompiled from: D:\myeclipsework\Kingdeeoa\webapp\WEB-INF\lib\license.jar
	Total time: 91 ms
	Jad reported messages/errors:
	Exit status: 0
	Caught exceptions:
*/